package com.java.digitalsignagesystem.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java.digitalsignagesystem.entity.*;
import com.java.digitalsignagesystem.service.OperateService;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * (Operate)表控制层
 *
 * @author makejava
 * @since 2023-08-24 10:56:12
 */
@RestController
@RequestMapping("/operate")
public class OperateController {
    /**
     * 服务对象
     */
    @Resource
    private OperateService operateService;

    //@ApiOperation("通过Id查询自己的关注列表")
    @GetMapping("/concernList")
    public Result concernList(@RequestParam(value = "num")int num,
                       @RequestParam(value = "userId")String userId,
                       @RequestParam(value = "theme",required = false)String theme){
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 计算起始时间，减去24小时
        LocalDateTime startTime = currentDateTime.minusHours(24);
        TimeRange timeRange = TimeRange.valueOf(num);
        switch (timeRange) {
            case LAST_24_HOURS:
                // 执行近24小时查询
                List<Operate> operates = operateService.selectConcernByUserId(userId,theme,startTime,currentDateTime);
                return Result.success(operates);
            case LAST_WEEK:
                // 执行近一个周查询
                startTime=currentDateTime.minusWeeks(1);
                List<Operate> operates1 = operateService.selectConcernByUserId(userId,theme,startTime,currentDateTime);
                return Result.success(operates1);

            case LAST_MONTH:
                // 执行近一个月查询
                startTime=currentDateTime.minusMonths(1);
                List<Operate> operates2 = operateService.selectConcernByUserId(userId,theme,startTime,currentDateTime);
                return Result.success(operates2);

            case LAST_MONTH6:
                startTime=currentDateTime.minusMonths(6);
                List<Operate> operates3 = operateService.selectConcernByUserId(userId,theme,startTime,currentDateTime);
                return Result.success(operates3);
            case LAST_NULL:
                startTime=null;
                currentDateTime=null;
                List<Operate> operates4 = operateService.selectConcernByUserId(userId,theme,startTime,currentDateTime);
                return Result.success(operates4);

            default:
                // 处理无效的时间范围值
                return Result.error("失败");

        }

    }



    //@ApiOperation("通过Id查询自己的收藏列表")
    @GetMapping("/collectList")
    public Result collectList(@RequestParam(value = "num")int num,
                       @RequestParam(value = "userId")String userId,
                       @RequestParam(value = "theme",required = false)String theme){
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 计算起始时间，减去24小时
        LocalDateTime startTime = currentDateTime.minusHours(24);
        TimeRange timeRange = TimeRange.valueOf(num);
        switch (timeRange) {
            case LAST_24_HOURS:
                // 执行近24小时查询
                List<Operate> operates = operateService.selectCollectByUserId(userId,theme,startTime,currentDateTime);
                return Result.success(operates);
            case LAST_WEEK:
                // 执行近一个周查询
                startTime=currentDateTime.minusWeeks(1);
                List<Operate> operates1 = operateService.selectCollectByUserId(userId,theme,startTime,currentDateTime);
                return Result.success(operates1);

            case LAST_MONTH:
                // 执行近一个月查询
                startTime=currentDateTime.minusMonths(1);
                List<Operate> operates2 = operateService.selectCollectByUserId(userId,theme,startTime,currentDateTime);
                return Result.success(operates2);

            case LAST_MONTH6:
                startTime=currentDateTime.minusMonths(6);
                List<Operate> operates3 = operateService.selectCollectByUserId(userId,theme,startTime,currentDateTime);
                return Result.success(operates3);
            case LAST_NULL:
                startTime=null;
                currentDateTime=null;
                List<Operate> operates4 = operateService.selectCollectByUserId(userId,theme,startTime,currentDateTime);
                return Result.success(operates4);

            default:
                // 处理无效的时间范围值
                return Result.error("失败");

        }

    }


   //@OperateLog(value = "关注、收藏")
   // @ApiOperation("关注收藏操作接口")
    @PostMapping("/add")
    public Result add(@RequestBody Operate operate) {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        operate.setCreateTime(formatter.format(date));
        if(operate!=null) {
            List<Operate> operates = operateService.selectByDataId(operate.getIndicatorId());
            if(operates!=null) {
                for(int i=0;i<operates.size();i++) {
                    operateService.deleteById(operates.get(i).getId());
                }
            }
            operateService.insert(operate);
            return Result.success();
        }
        return Result.error("失败");
    }

   // @ApiOperation("根据传入专题名称获取数据")
    @GetMapping("/dataList")
    public Result dataList(@RequestParam("theme")String theme,@RequestParam("userId")String userId) throws IOException {
        String data = operateService.getData(theme);
        List<ExampleEntity> entities = new ArrayList<>();
        JSONArray jsonArray = JSON.parseArray(data);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String dataString = jsonObject.getString("data");
            List<Data> dataList = JSON.parseArray(dataString, Data.class);
            for (int j=0;j<dataList.size();j++){
                List<Operate> operates = operateService.selectByUserId(userId);
                for(int k=0;k<operates.size();k++){
                    if(Objects.equals(operates.get(k).getIndicatorId(), dataList.get(j).getIndicator_id())){
                        dataList.get(j).setCollect(true);
                    }
                }
            }
            ExampleEntity entity = new ExampleEntity();
            entity.setData(dataList);
            entity.setName(jsonObject.getString("name"));
            entity.setSource_dept_name(jsonObject.getString("source_dept_name"));

            entities.add(entity);
        }
        return Result.success(entities);
    }


}

